<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.sql.*"%>
<%@ page import="java.util.Vector"%>
<%@ page import="USER.UserForm;"%>
<jsp:useBean id="dbConnection" class="DB.DBConnection" scope="application"/>

<%
	String username=request.getParameter("username");
	String password=request.getParameter("userpwd");
	//String room=request.getParameter("room");
	dbConnection.getConnection();
	String sql="select * from usr_info where usr_name='"+username+"'and usr_pwd='"+password+"'";
	ResultSet rs_user=dbConnection.executeQuery(sql);
	if(rs_user.next()){
		session.setAttribute("username",rs_user.getString("usr_name"));
		session.setAttribute("userpwd",rs_user.getString("usr_pwd"));
		//session.setAttribute("room",room);
		boolean flag=true;
		Vector<UserForm> temp=(Vector<UserForm>)application.getAttribute("myuser");
		if(application.getAttribute("myuser")==null){
			temp=new Vector<UserForm>();
		}
		for(int i=0;i<temp.size();i++){
			UserForm tempuser=(UserForm)temp.elementAt(i);
			if(tempuser.username.equals(username)){
				out.println("<script language='javascript'>alert('该用户已经登录');window.location.href='index.jsp';</script>");
            flag=false;
        	}
		}
		UserForm mylist=new UserForm();
		mylist.username=username;
		//保存当前登录的用户名
		session.setAttribute("username",username);
		application.setAttribute("ul",username);
		Vector<UserForm> myuser=(Vector<UserForm>)application.getAttribute("myuser");
		if(myuser==null){
			myuser=new Vector<UserForm>();
		}
		if(flag){
			myuser.addElement(mylist);
		}
		application.setAttribute("myuser",myuser);
		
	    response.sendRedirect("/209ChatRoom/chat/main.jsp");
%> 
<%		
	}
	else{
%>
	<script>
		alert("用户名或密码错误！");
		window.navigate("/209ChatRoom/index.jsp");
	</script>
<%
	} 
	dbConnection.closeConn();
%>